/*
 * Copyright (C) 2015 yixun All Rights Reserved.
 * 
 * CustomerPremisesController.java
 */
package com.yixun.qfbao.xf.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yixun.common.controller.BaseController;
import com.yixun.common.exception.YIXUNExceptionCode;
import com.yixun.common.exception.YIXUNUNCheckedException;
import com.yixun.common.page.Page;
import com.yixun.common.page.PageUtil;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.xf.controller.vo.CustomerPremisesVo;
import com.yixun.qfbao.xf.model.CustomerPremises;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesDto;
import com.yixun.qfbao.xf.service.CustomerPremisesService;

/**
 * [文件名称]<br>
 * CustomerPremisesController.java<br>
 * <br>
 * [文件描述]<br>
 * 客户报备信息 Controller<br>
 * <br>
 * [修改记录]<br>
 * 2015-10-14 15:34:13 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Controller
@RequestMapping("/xf/customerpremises")
public class CustomerPremisesController extends BaseController {
	@Autowired
	private CustomerPremisesService mservice;
	@Autowired
	private CompanyService companyService;
	private static Logger log = Logger.getLogger(CustomerPremisesController.class);
	
	@RequestMapping({"/manage"})
	public String index(Model model){
		return "/xf/customerPremises/manage";
	}
	
	@RequestMapping("/list")
	public @ResponseBody Page<CustomerPremisesDto> indexJson(CustomerPremisesVo entity,HttpServletResponse response,int page,int rows){
		Page<CustomerPremisesDto> pageModel = null;
		try {
			entity.setOrders("t.create_time desc");
			pageModel = mservice.getMyPageModel(entity.toDto(),PageUtil.begin(page,rows),rows);
			if(pageModel!=null){
				List<CustomerPremisesDto> lists = pageModel.getRows();
				for(CustomerPremises customerPremises:lists){
					Company company =companyService.selectById(customerPremises.getCompanyId());
					if(company!=null){
						customerPremises.setDevCompanyName(company.getCompanyName());
					}
				}
			}
		}catch (YIXUNUNCheckedException e) {
			log.error("分页信息失败 {}",e);
			pageModel = new Page<CustomerPremisesDto>();
			pageModel.setErrorMsg(e.getCode()+":"+e.getOutMsg());
		}catch (Exception e) {
			log.error("分页信息失败: {}",e);
			pageModel = new Page<CustomerPremisesDto>();
			pageModel.setErrorMsg(YIXUNExceptionCode.UNKNOW_EXCEPTON.getDesout());
		}
		return pageModel;
	}
	
	@RequestMapping("/view")
	public String formView(Model model,CustomerPremisesVo entity,HttpServletResponse response,Boolean isBack) throws IOException{
		try {
			if(isBack!=null&&isBack.booleanValue()){
				model.addAttribute("model",entity);
			}else{
				Page<CustomerPremisesDto> pageModel  = mservice.getMyPageModel(entity.toDto(),0,10);
				List<CustomerPremisesDto> lists = pageModel.getRows();
				for(CustomerPremises customerPremises:lists){
					Company company =companyService.selectById(customerPremises.getCompanyId());
					if(company!=null){
						customerPremises.setDevCompanyName(company.getCompanyName());
					}
				}
				model.addAttribute("model",lists.get(0));
			}
		}catch (YIXUNUNCheckedException e) {
			log.error("创建失败: {}",e);
		}catch (Exception e) {
			log.error("创建失败: {}",e);
		}
		return "/xf/customerPremises/save_edit";
	}
	
	@RequestMapping("/save_edit")
	public String saveOrUpdate(Model model,CustomerPremisesVo entity,HttpServletResponse response) throws IOException{
		String errorMsg = null;
		try {
			mservice.saveOrUpdate(entity);
		}catch (YIXUNUNCheckedException e) {
			log.error("编辑失败: {}",e);
			errorMsg = e.getCode()+":"+e.getOutMsg();
		}catch (Exception e) {
			errorMsg = YIXUNExceptionCode.UNKNOW_EXCEPTON.getDesout();
			log.error("编辑失败: {}",e);
		}
		if(StringUtils.isNotBlank(errorMsg)){
			model.addAttribute("errorMsg", errorMsg);
			return formView(model,entity,response,true);
		}
		return "forward:manage";
	}
	
	@RequestMapping("/del")
	public @ResponseBody Map<String,Object> destroy(CustomerPremisesVo entity,HttpServletResponse response) throws IOException{
		Map<String,Object> res = new HashMap<String, Object>();
		try {
			int destroyCount = mservice.destroy(entity.toDto());
			res.put("count",destroyCount);
			res.put("row",entity.toDto());
		}catch (YIXUNUNCheckedException e) {
			log.error("删除失败: {}",e);
			res.put("errorMsg",e.getCode()+":"+e.getOutMsg());
		}catch (Exception e) {
			log.error("删除失败: {}",e);
			res.put("errorMsg",YIXUNExceptionCode.UNKNOW_EXCEPTON.getDesout());
		}
		return res;
	}
}
